<template>
  <div>
  <!-- 按钮和搜索栏容器 -->
  <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;">
    <div style="display: flex; gap: 5px;">
      <el-button v-show="returnButton" @click="returnLists()"><el-icon><Back /></el-icon></el-button>
    </div>
    <!-- 搜索栏 -->
    <div>
      <el-input
          v-model="searchname"
          placeholder="请输入会议室名称、员工名称、维修原因"
          style="width: 250px;"
          @keyup.enter="search()"
      >
        <template #append>
          <el-button @click="search()"><el-icon><Search /></el-icon></el-button>
        </template>
      </el-input>
    </div>
  </div>

  <el-table id="mytable" :data="filteredData" stripe border style="width: 100%">
    <el-table-column fixed prop="id" label="会议室id" width="80" align="center"/>
    <el-table-column prop="roomName" label="会议室名称" width="150" align="center"/>
    <el-table-column prop="staffUsername" label="员工名字" align="center"/>
    <el-table-column prop="reason" label="维护原因描述" align="center"/>
    <el-table-column label="创建时间" align="center">
       <template #default="scope">
          {{ formatCreateTime(scope.row.createTime) }}
       </template>
    </el-table-column>
    <el-table-column label="维护开始时间" align="center">
       <template #default="scope">
          {{ formatCreateTime(scope.row.startTime) }}
       </template>
    </el-table-column>
    <el-table-column label="维护结束时间" align="center">
       <template #default="scope">
          {{ formatCreateTime(scope.row.endTime) }}
       </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <!-- 分页组件 -->
  <el-pagination
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    :page-size="pageSize"
    :total="totalCount"
  >
  </el-pagination>
  </div>
</template>

<script>
import axios from "axios";
import { Search } from '@element-plus/icons-vue';
import { Back } from "@element-plus/icons-vue";
export default {
  name: "LostAndFoundList",
  data() {
    return {
      listData: [],
      filteredData: [],
      searchname: '',
      returnButton:false,
      currentPage:1,
      pageSize:10,
      totalCount:0,    
    }
  },
  components :{
     Search,
     Back,
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    handleCurrentChange(page){
        this.currentPage=page;
        if(!this.searchname){
        this.fetchData();
        }else{
          this.search();
        }
    },
    fetchData(){
        axios.get("/admin/record",{
          params:{
            pageNum:this.currentPage,
            pageSize:this.pageSize,
          }
        })
        .then(res=>{
        
          this.listData=res.data.data.records;
          this.totalCount=res.data.data.total;
          if (Array.isArray(this.listData)) {
            this.filteredData = this.listData;
          } else {
              console.error('加载错误');
          }
        }).catch(err=>{
          this.$message.error(err.data.code||"获取列表失败");
        })
    },
    search() {
      this.returnButton=true;
      axios.get('/admin/record',{
          params:{
            keyword:this.searchname,
            pageNum:this.currentPage,
            pageSize:this.pageSize,
          }
        })
      .then(res =>{
        if(res.data.code===200){
          this.filteredData=res.data.data.records;
          this.totalCount=res.data.data.total;
          this.$message.success("查询成功");
        }else {
          this.$message.error(res.data.msg||"查询失败");
        }
      })
    },
    returnLists(){
      this.returnButton=false;
      this.searchname=null;
      this.currentPage=1;
      this.fetchData();
    },
    goBackToList() {
      this.fetchData();
    },
    formatCreateTime(time){
      if(time){
        return time.replace('T',' ');
      }
      return '无'
    },
  },
}

</script>

<style scoped>
.clicked-s {
  background-color: green;
  color: white;
}
</style>